package com.muyu.danmo.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.PageUtils;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.danmo.service.IDanmoOrderService;
import com.muyu.system.domain.Bo.DanmoOrderBo;
import com.muyu.system.domain.DanmoOrder;
import com.muyu.system.domain.Vo.DanmoOrderVo;
import com.muyu.danmo.utils.JPushUtils;
import com.muyu.system.domain.Vo.OrderCountVO;
import com.muyu.system.domain.Vo.OrderListVO;
import com.muyu.system.domain.Vo.StatusCountVO;
import com.muyu.system.domain.dto.OrderCountDTO;
import com.muyu.system.domain.dto.OrderListDTO;
import com.muyu.system.domain.dto.StatusCountDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 订单Controller
 *
 * @author yangle
 * @date 2025-01-18
 */
@RestController
@RequestMapping("/order")
public class DanmoOrderController extends BaseController {
    @Autowired
    private IDanmoOrderService danmoOrderService;

    @Autowired
    private JPushUtils jPushUtils;

    /**
     * 查询订单列表
     */
//    @RequiresPermissions("system:order:list")
    @PostMapping("/find/list")
    public Result<TableDataInfo<DanmoOrderVo>> list(@RequestBody DanmoOrder danmoOrder) {
        startPage();
        List<DanmoOrderVo> list = danmoOrderService.selectDanmoOrderList(danmoOrder);
        return getDataTable(list);
    }

    /**
     * 商家订单统计
     */
    @PostMapping("/orderCountOfStore")
    public Result orderCountOfStore(@RequestBody DanmoOrder danmoOrder) {
        return danmoOrderService.orderCountOfStore(danmoOrder);
    }

    /**
     * 导出订单列表
     */
//    @RequiresPermissions("system:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoOrder danmoOrder) {
        List<DanmoOrderVo> list = danmoOrderService.selectDanmoOrderList(danmoOrder);
        List<DanmoOrder> danmoOrders = BeanUtil.copyToList(list, DanmoOrder.class);
        ExcelUtil<DanmoOrder> util = new ExcelUtil<DanmoOrder>(DanmoOrder.class);
        util.exportExcel(response, danmoOrders, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
//    @RequiresPermissions("system:order:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return success(danmoOrderService.selectDanmoOrderById(id));
    }

    /**
     * 新增订单
     */
//    @RequiresPermissions("system:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody DanmoOrderBo danmoOrderbo) {
        return danmoOrderService.insertDanmoOrder(danmoOrderbo);
    }




    /**
     * 修改订单
     */
//    @RequiresPermissions("system:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoOrder danmoOrder) {
        return danmoOrderService.updateDanmoOrder(danmoOrder);
    }

    /**
     * 删除订单
     */
//    @RequiresPermissions("system:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return danmoOrderService.deleteDanmoOrderByIds(ids);
    }



    /**
     * 个人中心-订单列表
     * @param orderListDTO
     * @return
     */
    @PostMapping("/order/list")
    public Result<PageInfo<OrderListVO>> orderList(@RequestBody OrderListDTO orderListDTO){
        IPage<OrderListVO> orderListVOIPage = danmoOrderService.orderList(orderListDTO);
        return Result.success(PageUtils.convertToPageInfo(orderListVOIPage));
    }

    /**
     * 业务系统订单号，查询订单信息
     * @param orderSn
     * @return
     */
    @GetMapping("/orderSn/{orderSn}")
    public Result<OrderListVO> findByOrderSn(@PathVariable String orderSn){
        OrderListVO byOrderSn = danmoOrderService.findByOrderSn(orderSn);
        return Result.success(byOrderSn);
    }


    /**
     * 订单统计
     * @param orderCountDTO
     * @return
     */
    @PostMapping("/count")
    public Result<OrderCountVO>count(@RequestBody @Valid OrderCountDTO orderCountDTO){
        return Result.success(danmoOrderService.count(orderCountDTO));
    }

    /**
     * 订单状态统计
     * @return
     */
    @PostMapping("/status/count")
    public Result<StatusCountVO> statusCount(@RequestBody @Valid StatusCountDTO statusCountDTO){
        return Result.success(danmoOrderService.statusCount(statusCountDTO));
    }





}
